package com.qianfeng.bob.xiexiaobo;

import android.content.Intent;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

import com.qianfeng.bob.xiexiaobo.view.MyCircleImageView;

import java.io.IOException;

public class TakePictureActivity extends AppCompatActivity implements SurfaceHolder.Callback, Camera.PictureCallback {
    private SurfaceView mSurfaceView;
    private Camera mCamera;
    private MyCircleImageView mCirclrView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_picture);
        initView();
        setData();
    }


    /**
     * 初始化数据
     */
    private void setData() {
        mSurfaceView.getHolder().addCallback(this);
        mSurfaceView.getHolder().setKeepScreenOn(true);
        mCamera = Camera.open(0);
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mCirclrView = ((MyCircleImageView) findViewById(R.id.imageView));
        mSurfaceView = ((SurfaceView) findViewById(R.id.sv_takepic_test));
    }
    public void onClick(View view){
        if (view!=null){
            //拍照
           mCamera.takePicture(null,null,this);
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mCamera!=null){
            try {
                mCamera.setPreviewDisplay(holder);//提供展示平台
                mCamera.setDisplayOrientation(90);//旋转90*
                mCamera.startPreview();//开始展示
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {

    }

    /**
     * 获取到了图片通过intent返回图片
     * @param data
     * @param camera
     */
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        //获取图片并返回图片
        Intent intent = new Intent();
        intent.putExtra("data",data);
        setResult(1,intent);
        finish();
    }
}
